#include "tray-icon-item.h"
#include "status-notifier-item.h"
#include <QDebug>

namespace UkuiSystemTray {
QIcon extractIcon(const QIcon&icon, const QIcon&defaultValue)
{
    return icon.isNull() ? defaultValue : icon;
}

class TrayIconItemPrivate
{
public:
    QString m_source;
    UkuiSni::StatusNotifierItem* m_item = nullptr;
    bool m_fixed{false};
    bool m_recordOrder{false};
};

TrayIconItem::TrayIconItem(const QString&itemSource, QObject* parent) : QObject(parent), d(new TrayIconItemPrivate)
{
    d->m_source = itemSource;
    d->m_item = UkuiSni::StatusNotifierHost::self()->itemForService(d->m_source);
    connect(d->m_item, &UkuiSni::StatusNotifierItem::dataUpdated, this, &TrayIconItem::dataUpdated);
    connect(d->m_item, &UkuiSni::StatusNotifierItem::contextMenuReady, this, &TrayIconItem::contextMenuReady);
    connect(d->m_item, &UkuiSni::StatusNotifierItem::activateResult, this, &TrayIconItem::activateResult);
}

QString TrayIconItem::service() const
{
    if (d->m_item) {
        return d->m_item->service();
    }
    return {};
}

QIcon TrayIconItem::attentionIcon() const
{
    if (d->m_item) {
        return d->m_item->attentionIcon().isNull() ? icon() : d->m_item->attentionIcon();
    }
    return {};
}

QString TrayIconItem::attentionIconName() const
{
    if(d->m_item) {
        return d->m_item->attentionIconName();
    }
    return {};
}

QString TrayIconItem::attentionMovieName() const
{
    if(d->m_item) {
        return d->m_item->attentionMovieName();
    }
    return {};
}

QString TrayIconItem::category() const
{
    if(d->m_item) {
        return d->m_item->category();
    }
    return {};
}

QIcon TrayIconItem::icon() const
{
    if (d->m_item) {
        return d->m_item->icon().isNull()? QIcon::fromTheme("application-x-executable") : d->m_item->icon();
    }
    return {};
}

QString TrayIconItem::iconName() const
{
    if(d->m_item) {
        return d->m_item->iconName();
    }
    return {};
}

QString TrayIconItem::iconThemePath() const
{
    if(d->m_item) {
        return d->m_item->iconThemePath();
    }
    return {};
}

QString TrayIconItem::id() const
{
    if(d->m_item) {
        return d->m_item->id();
    }
    return {};
}

bool TrayIconItem::itemIsMenu() const
{
    if(d->m_item) {
        return d->m_item->itemIsMenu();
    }
    return false;
}

QIcon TrayIconItem::overlayIcon() const
{
    if(d->m_item) {
        return d->m_item->overlayIcon();
    }
    return {};
}

QString TrayIconItem::overlayIconName() const
{
    if(d->m_item) {
        return d->m_item->overlayIconName();
    }
    return {};
}

QString TrayIconItem::status() const
{
    if(d->m_item) {
        return d->m_item->status();
    }
    return "Passive";
}

QString TrayIconItem::title() const
{
    if(d->m_item) {
        return d->m_item->title();
    }
    return {};
}

QString TrayIconItem::toolTipSubTitle() const
{
    if(d->m_item) {
        return d->m_item->toolTipSubTitle();
    }
    return {};
}

QString TrayIconItem::toolTipTitle() const
{
    if(d->m_item) {
        return d->m_item->toolTipTitle();
    }
    return {};
}

QString TrayIconItem::windowId() const
{
    if(d->m_item) {
        return d->m_item->windowId();
    }
    return {};
}
bool TrayIconItem::fixed() const
{
    return d->m_fixed;
}
void TrayIconItem::setFixed(bool fixed)
{
    d->m_fixed = fixed;
}

bool TrayIconItem::recordOrder() const
{
    return d->m_recordOrder;
}

void TrayIconItem::setRecordOrder(bool recordOrder)
{
    d->m_recordOrder = recordOrder;
}

QString TrayIconItem::source() const
{
    return d->m_source;
}

void TrayIconItem::contextMenu(int x, int y)
{
    d->m_item->contextMenu(x, y);
}

void TrayIconItem::activate(int x, int y)
{
    d->m_item->activate(x, y);
}

void TrayIconItem::secondaryActivate(int x, int y)
{
    d->m_item->secondaryActivate(x, y);
}

void TrayIconItem::scroll(int delta, const QString &direction)
{
    d->m_item->scroll(delta, direction);
}

void TrayIconItem::provideXdgActivationToken(const QString &token)
{
    d->m_item->provideXdgActivationToken(token);
}
}
